//
//  AppDelegate.m
//  NotepadApp
//
//  Created by yobet on 2018/4/12.
//  Copyright © 2018年 HJ. All rights reserved.
//
#define GDMapAppKey @"fa2e556a0eb2047f15ebc30c9654ec7e"
#import "AppDelegate.h"
#import "BDGuideInterfaceView.h"
#import "IQKeyboardManager.h"
#import <AudioToolbox/AudioToolbox.h>

@interface AppDelegate ()<UIAlertViewDelegate> {
    SystemSoundID ID;
}

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [AMapServices sharedServices].apiKey = GDMapAppKey;

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//    BOOL isPacking = [[[NSUserDefaults standardUserDefaults]objectForKey:@"packing"] boolValue];
//    if (isPacking) {
//        [self.window setBackgroundColor:[UIColor whiteColor]];
//        self.window.rootViewController = [[WebViewController alloc]init];
//        [self.window makeKeyAndVisible];
//    }else {
        self.window.rootViewController = [[NotepadTabBarVC alloc]init];
        [self.window makeKeyAndVisible];
//    }
    
    
    //键盘设置
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    [[IQKeyboardManager sharedManager] setEnable:YES];
    manager.shouldResignOnTouchOutside = YES; // 控制点击背景是否收起键盘
    manager.enableAutoToolbar = YES;
    manager.shouldShowTextFieldPlaceholder = YES;
    manager.keyboardDistanceFromTextField = 2;
    
//    [self guideInterfaceSet];
    
    
    //注册本地通知
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // iOS8
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
        [application registerUserNotificationSettings:setting];
    }
    
    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
        // 这里添加处理代码
        NSLog(@"notificationnotificationnotification");
    }
    
    
    return YES;
}
// 启动页
- (void)guideInterfaceSet {
//    if (USERMANAGER.isGuideShow) {
//        return;
//    }
//    USERMANAGER.guideShow = YES;
    
    NSArray *images = @[@"",@"",@""];
    BDGuideInterfaceView *guideInterfaceView = [[BDGuideInterfaceView alloc] initWithFrame:[UIScreen mainScreen].bounds imageDataArray:images];
    
    [self.window addSubview:guideInterfaceView];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
/// 程序没有被杀死（处于前台或后台），点击通知后会调用此程序
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    // 这里添加处理代码
    NSLog(@"%@",notification);
    NSString *message = notification.alertBody;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"确定", nil ,nil];
    [alert show];
    [self playVoice];
}

//播放系统声音
- (void)playVoice{
    //开启音效
    NSString *path =[[NSBundle mainBundle]pathForResource:@"jiancha" ofType:@"m4a"];
    NSURL *url = [NSURL fileURLWithPath:path];
    // 声明需要播放的音频文件ID[unsigned long]
    // 创建系统声音，同时返回一个ID
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &ID);
    //播放
    AudioServicesPlaySystemSound(ID);
}

- (void)stopVoice{
    AudioServicesDisposeSystemSoundID(ID);
}


#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [self stopVoice];
}

@end
